﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Text;
using System.Web.Services;

using CE.Database;
using CE.Mail;
using CE.Global;
using CE.CarInsurance;
using CE.User;
using CE.Security;

public partial class car_insurance_car_quote : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    CE.User.Profile _profile = null;
    CE.User.ProfileDB _profiledb = null;
    Contact _contact = null;
    ContactDB _contactdb = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie _cookie = Request.Cookies.Get("GGMobileQuote");
            hdnUserLoginStatus.Value = "0";
            if (_cookie != null)
            {
                hdnUserLoginStatus.Value = "1";
            }
            LoadTitles();
            LoadCarYear();
            LoadCommenceMonth();
            txtSumInsured.Value = "0";
            //Sakthi Added on 13 Jun 2014 - Start
            Uri theRealURL = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl);
            string rq_type = HttpUtility.ParseQueryString(theRealURL.Query).Get("type");
            if (rq_type != null && rq_type != string.Empty)
            {
                hdnURLFrom.Value = Server.HtmlEncode(rq_type.ToString()).Trim().ToLower();
                if (rq_type.ToLower() == "hmm")
                {
                    LogUserViewedHistory("CQHM", "Car Quote from Mobile Home Page");
                }
                else if (rq_type.ToLower() == "bcim")
                {
                    LogUserViewedHistory("CQBM", "Car Quote from Mobile Best Car Insurance Page");
                }
            }
            //Sakthi Added on 13 Jun 2014 - End
        }
    }

    public void LoadCarYear()
    {
        try
        {
            string result = "";
            int _yearCount = 14;
            int _startYear = DateTime.Now.Year - _yearCount;
            int _endYear = DateTime.Now.Year;
            DataTable _dtblYear = new DataTable();
            _dtblYear.Clear();
            _dtblYear.Columns.Add("YearId");
            _dtblYear.Columns.Add("Year");
            for (int i = _endYear; i >= _startYear; i--)
            {
                DataRow _dtblRow = _dtblYear.NewRow();
                _dtblRow["YearId"] = i.ToString();
                if (Session["Language"] != null && Session["Language"].ToString() != "th-TH")
                {
                    _dtblRow["Year"] = i.ToString();
                }
                else
                {
                    _dtblRow["Year"] = i.ToString() + " - พ.ศ. " + (i + 543).ToString();
                }
                _dtblYear.Rows.Add(_dtblRow);
            }
            result = "<ul class='select-box-2' id='CarYear'>";
            foreach (DataRow row in _dtblYear.Rows)
            {
                result += "<li class='text-link' rel='" + row["YearId"].ToString() + "'>";
                result += row["Year"].ToString();
                result += "</li>";
            }
            ltrCarYear.InnerHtml = result += "</ul>";
        }
        catch (Exception ex)
        {
        }
    }

    public void LoadCommenceMonth()
    {
        try
        {
            string result = "";
            DataTable _dtblMonth = new DataTable();
            _dtblMonth.Clear();
            _dtblMonth.Columns.Add("MonthId");
            _dtblMonth.Columns.Add("Month");
            DateTime dtNow = new DateTime();
            dtNow = General.GetUTCDateTime().AddDays(14);

            DateTime dtEndMonth = new DateTime();
            dtEndMonth = General.GetUTCDateTime().AddDays(120);

            //int mthDiff = 12 - dtNow.Month;
            for (int m = 0; m <= 11; m++)
            {
                //DateTime dtTemp = dtNow.AddMonths(m);
                //string strDate = dtTemp.Date.Month.ToString("00") + dtTemp.Date.Year.ToString("0000");
                //ddlCoverageComMonth.Items.Add(new ListItem(dtTemp.ToString("MMMM yyyy", ci), strDate));

                DateTime dtTemp = dtNow.AddMonths(m);
                string strDate = dtTemp.Date.Month.ToString("00") + dtTemp.Date.Year.ToString("0000");

                DataRow _dtblRow = _dtblMonth.NewRow();
                _dtblRow["MonthId"] = strDate.ToString();
                _dtblRow["Month"] = CommencementMonth(strDate, dtTemp);// dtTemp.ToString("MMMM yyyy").ToString();
                _dtblMonth.Rows.Add(_dtblRow);
            }
            result = "<ul class='select-box-2' id='Commencement'>";
            foreach (DataRow row in _dtblMonth.Rows)
            {
                result += "<li class='text-link' onclick='cm(this);' rel=" + row["MonthId"].ToString() + ">";
                result += row["Month"].ToString();
                result += "</li>";
            }
            ltrCommencement.InnerHtml = result += "</ul>";
        }
        catch (Exception ex)
        {
        }
    }

    public string CommencementMonth(string Selected, DateTime SelectDate)
    {
        try
        {
            string _strCommencementMonth = SelectDate.ToString("MMMM yyyy").ToString(); ;
            string _strmonth = Selected.Substring(0, 2);
            string _stryear = Selected.Substring(2, 4);
            int _month = 0;
            int _year = 0;
            if (!int.TryParse(_strmonth, out _month))
            {
                _month = 0;
            }
            if (!int.TryParse(_stryear, out _year))
            {
                _year = 0;
            }
            if (Session["Language"] != null && Session["Language"].ToString() != "th-TH")
            {
                _strCommencementMonth = SelectDate.ToString("MMMM yyyy").ToString();
            }
            else
            {
                if (_month == 1)
                {
                    _strCommencementMonth = Resources.MonthResource.January + " " + (_year + 543).ToString();
                }
                else if (_month == 2)
                {
                    _strCommencementMonth = Resources.MonthResource.February + " " + (_year + 543).ToString();
                }
                else if (_month == 3)
                {
                    _strCommencementMonth = Resources.MonthResource.March + " " + (_year + 543).ToString();
                }
                else if (_month == 4)
                {
                    _strCommencementMonth = Resources.MonthResource.April + " " + (_year + 543).ToString();
                }
                else if (_month == 5)
                {
                    _strCommencementMonth = Resources.MonthResource.May + " " + (_year + 543).ToString();
                }
                else if (_month == 6)
                {
                    _strCommencementMonth = Resources.MonthResource.June + " " + (_year + 543).ToString();
                }
                else if (_month == 7)
                {
                    _strCommencementMonth = Resources.MonthResource.July + " " + (_year + 543).ToString();
                }
                else if (_month == 8)
                {
                    _strCommencementMonth = Resources.MonthResource.August + " " + (_year + 543).ToString();
                }
                else if (_month == 9)
                {
                    _strCommencementMonth = Resources.MonthResource.September + " " + (_year + 543).ToString();
                }
                else if (_month == 10)
                {
                    _strCommencementMonth = Resources.MonthResource.October + " " + (_year + 543).ToString();
                }
                else if (_month == 11)
                {
                    _strCommencementMonth = Resources.MonthResource.November + " " + (_year + 543).ToString();
                }
                else if (_month == 12)
                {
                    _strCommencementMonth = Resources.MonthResource.December + " " + (_year + 543).ToString();
                }
            }
            return _strCommencementMonth;
        }
        catch (Exception ex)
        {
            return Selected;
        }
    }
    private void LoadTitles()
    {
        lblCarYearTitle.Text = Resources.CarMenu.CarYearTitle;
        lblCarMakeTitle.Text = Resources.CarMenu.CarMakeTitle;
        lblCarModelTitle.Text = Resources.CarMenu.CarModelTitle;
        lblCommencementTitle.Text = Resources.CarMenu.CommencementTitle;
        ibtnNewQuote.ImageUrl = Resources.CarMenu.ButtonNewQuoteUrl;
    }


    [WebMethod]
    public static string CarYearClickChanged(string Year)
    {
        try
        {
            string result = "";
            MyDatabase _db = new MyDatabase();
            DataTable _dt = new DataTable("tblMake");
            SortedList _spList = new SortedList();
            _spList.Add("@Year", Year);
            _dt = _db.GetDataTableBySP("sprocGetCarMakeTHListByYear", _spList);
            if (_dt != null && _dt.Rows.Count > 0)
            {
                result = "<ul class='select-box-2' id='CarMake'>";
                foreach (DataRow row in _dt.Rows)
                {
                    result += "<li class='text-link' onclick='my(this);' rel=" + row["MakeCode"].ToString() + ">";
                    result += row["MakeDesc"].ToString();
                    result += "</li>";
                }
                result += "</ul>";
            }
            _dt = null;
            _db = null;
            return result;
        }
        catch (Exception ex)
        {
            return "";
        }
    }

    [WebMethod]
    public static string CarMakeClickChanged(string CarMake, string CarMakeDesc, string Year)
    {
        try
        {
            string result = "";
            string s = CarMakeDesc;
            MyDatabase _db = new MyDatabase();
            DataTable _dt = new DataTable("tblModelDesc");
            SortedList _spList = new SortedList();
            _spList.Add("@CarMake", CarMake);
            _spList.Add("@Year", Year);
            _dt = _db.GetDataTableBySP("sprocGetCarModelDescTHListByCarMakeCodeMobile", _spList);
            if (_dt != null && _dt.Rows.Count > 0)
            {
                result = "<ul class='select-box-2' id='CarModel'>";
                foreach (DataRow row in _dt.Rows)
                {
                    result += "<li class='text-link' onclick='mo(this);' rel=" + row["CarModelDesc"].ToString() + ">";
                    //result += row["CarModelDesc"].ToString();
                    result += row["CarModelDisplay"].ToString();
                    result += "</li>";
                }
                result += "</ul>";
            }
            _dt = null;
            _db = null;
            return result;
        }
        catch (Exception ex)
        {
            return "";
        }
    }

    [WebMethod]
    public static string CarModelClickChanged(string Year, string CarMake, string CarMakeDesc, string CarModel, string CarModelDesc)
    {
        try
        {
            string result = "";
            car_insurance_car_quote _objCarQuote = new car_insurance_car_quote();
            string[] _tempDesc = CarModelDesc.Split('~');
            string _model = "";
            string _body = "";
            string _cc = "";
            string _door = "";
            if (_tempDesc.Length == 5)
            {
                _model = _tempDesc[0];
                _body = _tempDesc[1];
                _cc = _tempDesc[2].Substring(0, _tempDesc[2].Length - 2);
                _door = _tempDesc[3];
            }
            else if (_tempDesc.Length == 6)
            {
                _model = _tempDesc[0] + " " + _tempDesc[1];
                _body = _tempDesc[2];
                _cc = _tempDesc[3].Substring(0, _tempDesc[3].Length - 2);
                _door = _tempDesc[4];
            }
            else if (_tempDesc.Length == 7)
            {
                _model = _tempDesc[0] + " " + _tempDesc[1] + " " + _tempDesc[2];
                _body = _tempDesc[3];
                _cc = _tempDesc[4].Substring(0, _tempDesc[4].Length - 2);
                _door = _tempDesc[5];
            }

            string SumInsured = "0";
            string SumInsuredFormated = "0";
            string VehicleType = "0";
            string GroupNo = "-1";
            MyDatabase _dbSI = new MyDatabase();
            DataTable _dtSI = new DataTable("tblModelBody");
            SortedList _spListSI = new SortedList();
            _spListSI.Add("@CarMake", CarMake);
            _spListSI.Add("@CarModel", _model);
            _spListSI.Add("@Body", _body);
            _spListSI.Add("@CC", _cc);
            _spListSI.Add("@Door", _door);
            _spListSI.Add("@Year", Year);
            _dtSI = _dbSI.GetDataTableBySP("sprocGetCarModelSumInsuredAndVehicleTypeTHListByYear", _spListSI);
            if (_dtSI != null && _dtSI.Rows.Count > 0)
            {
                SumInsured = "0";
                SumInsuredFormated = "0";
                VehicleType = "LC";
                SumInsured = _dtSI.Rows[0][0].ToString();
                VehicleType = _dtSI.Rows[0][1].ToString();
                SumInsuredFormated = Convert.ToInt32(SumInsured).ToString("#,000");
                SumInsured = string.Format("{0:#,0}", _dtSI.Rows[0][0]);
            }
            else
            {
                SumInsured = "0";
                SumInsuredFormated = "0";
                VehicleType = "LC";
            }
            _dtSI = null;
            _dbSI = null;

            MyDatabase _dbVG = new MyDatabase();
            DataTable _dtVG = new DataTable("tblModelGroup");
            SortedList _spListVG = new SortedList();
            _spListVG.Add("@CarMake", CarMake);
            _spListVG.Add("@CarModel", _model);
            _spListVG.Add("@Body", _body);
            _spListVG.Add("@CC", _cc);
            _spListVG.Add("@Door", _door);
            _dtVG = _dbVG.GetDataTableBySP("sprocGetCarModelGroupTH", _spListVG);
            if (_dtVG != null && _dtVG.Rows.Count > 0)
            {
                GroupNo = _dtVG.Rows[0][0].ToString();
            }
            else
            {
                GroupNo = "-1";
            }
            _dtVG = null;
            _dbVG = null;
            return result = SumInsured + ":" + SumInsuredFormated + ":" + VehicleType + ":" + GroupNo;
        }
        catch (Exception ex)
        {
            return "0:0:LC:-1";
        }
    }

    [WebMethod]
    public static string SaveMobileCarInquiry(string Year, string CarMake, string CarMakeDesc, string CarModel, string CarModelDesc, string URLFrom)
    {
        try
        {
            #region Declaration
            string _InquiryId = "-1";
            string _CarId = "-1";
            string _PolicyId = "-1";
            string _result = "-1:-1:-1";
            Int64 _carresult = 0;
            Int64 _policyresult = 0;
            Int64 _refceuserid = 0;
            string Commencemonth = General.GetUTCDateTime().AddDays(14).ToString("ddMMyyyy");
            string SumInsured = "0";
            string SumInsuredFormated = "0";
            string VehicleType = "110";
            string GroupNo = "-1";
            string CarVehicleType = "";
            CarDetailTH _car = new CarDetailTH();
            CarDetailTHDB _cardb = new CarDetailTHDB();

            ProfileDB _profiledb = new ProfileDB();
            Profile _profile = new Profile();

            Contact _contact = new Contact();
            ContactDB _contactdb = new ContactDB();
            #endregion

            car_insurance_car_quote _objCarQuote = new car_insurance_car_quote();
            string[] _tempDesc = CarModelDesc.Split('~');
            string _model = "";
            string _body = "";
            string _cc = "";
            string _door = "";
            if (_tempDesc.Length == 5)
            {
                _model = _tempDesc[0];
                _body = _tempDesc[1];
                _cc = _tempDesc[2].Substring(0, _tempDesc[2].Length - 2);
                _door = _tempDesc[3];
            }
            else if (_tempDesc.Length == 6)
            {
                _model = _tempDesc[0] + " " + _tempDesc[1];
                _body = _tempDesc[2];
                _cc = _tempDesc[3].Substring(0, _tempDesc[3].Length - 2);
                _door = _tempDesc[4];
            }
            else if (_tempDesc.Length == 7)
            {
                _model = _tempDesc[0] + " " + _tempDesc[1] + " " + _tempDesc[2];
                _body = _tempDesc[3];
                _cc = _tempDesc[4].Substring(0, _tempDesc[4].Length - 2);
                _door = _tempDesc[5];
            }

            MyDatabase _dbSI = new MyDatabase();
            DataTable _dtSI = new DataTable("tblModelBody");
            SortedList _spListSI = new SortedList();
            _spListSI.Add("@CarMake", CarMake);
            _spListSI.Add("@CarModel", _model);
            _spListSI.Add("@Body", _body);
            _spListSI.Add("@CC", _cc);
            _spListSI.Add("@Door", _door);
            _spListSI.Add("@Year", Year);
            _dtSI = _dbSI.GetDataTableBySP("sprocGetCarModelSumInsuredAndVehicleTypeTHListByYear", _spListSI);
            if (_dtSI != null && _dtSI.Rows.Count > 0)
            {
                SumInsured = "0";
                SumInsuredFormated = "0";
                CarVehicleType = "LC";
                SumInsured = _dtSI.Rows[0][0].ToString();
                CarVehicleType = _dtSI.Rows[0][1].ToString();
                SumInsuredFormated = Convert.ToInt32(SumInsured).ToString("#,000");
                SumInsured = string.Format("{0:#,0}", _dtSI.Rows[0][0]);
            }
            else
            {
                SumInsured = "0";
                SumInsuredFormated = "0";
                CarVehicleType = "LC";
            }
            _dtSI = null;
            _dbSI = null;

            MyDatabase _dbVG = new MyDatabase();
            DataTable _dtVG = new DataTable("tblModelGroup");
            SortedList _spListVG = new SortedList();
            _spListVG.Add("@CarMake", CarMake);
            _spListVG.Add("@CarModel", _model);
            _spListVG.Add("@Body", _body);
            _spListVG.Add("@CC", _cc);
            _spListVG.Add("@Door", _door);
            _dtVG = _dbVG.GetDataTableBySP("sprocGetCarModelGroupTH", _spListVG);
            if (_dtVG != null && _dtVG.Rows.Count > 0)
            {
                GroupNo = _dtVG.Rows[0][0].ToString();
            }
            else
            {
                GroupNo = "-1";
            }
            _dtVG = null;
            _dbVG = null;

            string strcDate = Commencemonth;
            int _icDay = Convert.ToInt32(strcDate.Substring(0, 2));
            int _icMonth = Convert.ToInt32(strcDate.Substring(2, 2));
            int _icYear = Convert.ToInt32(strcDate.Substring(4, 4));
            DateTime CommencementDate = new DateTime(_icYear, _icMonth, _icDay);
            CarModelDesc = CarModelDesc.Replace('~', ' ');
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocMobileCarInquiryDetailsInsertUpdateSingleItem";

            SortedList _spList = new SortedList();
            if (HttpContext.Current.Session[General.InquiryId] != null)
            {
                _spList.Add("@MobileCarInquiryId", Convert.ToInt64(HttpContext.Current.Session[General.InquiryId].ToString()));
            }
            else
            {
                _spList.Add("@MobileCarInquiryId", 0);
            }
            _spList.Add("@Name", "");
            _spList.Add("@EmailAddress", "");
            _spList.Add("@PhoneNumber", "");
            _spList.Add("@CarMakeDescription", CarMakeDesc);
            _spList.Add("@CarMakeCode", CarMake);
            _spList.Add("@CarModelDescription", CarModelDesc);
            _spList.Add("@CarModelCode", _model);
            _spList.Add("@CarYear", Year);
            _spList.Add("@CarId", 0);
            _spList.Add("@CarPolicyId", 0);
            _spList.Add("@RefCEUserId", 0);
            _spList.Add("@SumInsured", SumInsured);
            _spList.Add("@VehicleType", VehicleType);
            _spList.Add("@CarVehicleType", CarVehicleType);
            _spList.Add("@GroupNo", GroupNo);
            _spList.Add("@CommencementDate", CommencementDate);
            _spList.Add("@InsuranceClass", "");
            _spList.Add("@Step", 1);

            object _retObject = _db.ExecuteSP(_spName, _spList);
            if (_retObject != null)
            {
                _InquiryId = Convert.ToInt64(_retObject).ToString();
                HttpContext.Current.Session[General.InquiryId] = _retObject.ToString();
            }

            if (Convert.ToInt64(_InquiryId) > 0)
            {
                #region CarDetails
                if (HttpContext.Current.Session[General.CarId] != null)
                {
                    _car = _cardb.GetCar(Convert.ToInt64(HttpContext.Current.Session[General.CarId].ToString()), 0);
                    if (HttpContext.Current.Session[General.AgentId] != null)
                    {
                        _car.Info5 = "UPDATE";
                    }
                }
                else
                {
                    if (HttpContext.Current.Session[General.AgentId] != null)
                    {
                        _car.Info4 = "NEW";
                    }
                }

                if (_car == null)
                {
                    _car = new CarDetailTH();
                }

                if (HttpContext.Current.Session[CE.Global.General.UserId] != null)
                {
                    _refceuserid = Convert.ToInt64(HttpContext.Current.Session[CE.Global.General.UserId]);
                    _profile = _profiledb.GetProfile(_refceuserid);
                    _contact = _contactdb.GetContact(_refceuserid);
                    if (_profile != null && _contact != null)
                    {
                        _car.Info5 = _contact.Email;
                        _car.Info6 = _contact.Mobile;
                        _car.Info7 = _profile.SurName;
                        if (_profile.FirstName.Trim().Length > 0)
                        {
                            _car.Info8 = _profile.FirstName;
                        }
                        _car.Info9 = _profile.Gender.ToString();
                        _car.Info10 = _profile.DOB.ToString("dd-MMM-yyyy");
                        _car.Info11 = _profile.Occupation.ToString();
                        _car.Info12 = _profile.JobNature;
                        _car.Info13 = _profile.MaritalStatus.ToString();
                        _car.Info14 = "0";
                        _car.Info15 = _profile.Salutation;
                        _profile = null;
                        _contact = null;
                    }
                }

                _car.ManufactYear = Convert.ToInt32(Year);

                _car.CarMakeName = CarMake;
                string[] _tempDescCar = CarModelDesc.Split(' ');
                if (_tempDescCar.Length == 5)
                {
                    _car.CarModelName = _tempDescCar[0];
                    _car.Body = _tempDescCar[1];
                    _car.CC = Convert.ToInt32(_tempDescCar[2].Substring(0, _tempDescCar[2].Length - 2));
                    _car.Door = Convert.ToInt32(_tempDescCar[3]);
                }
                else if (_tempDescCar.Length == 6)
                {
                    _car.CarModelName = _tempDescCar[0] + " " + _tempDescCar[1];
                    _car.Body = _tempDescCar[2];
                    _car.CC = Convert.ToInt32(_tempDescCar[3].Substring(0, _tempDescCar[3].Length - 2));
                    _car.Door = Convert.ToInt32(_tempDescCar[4]);
                }
                else if (_tempDescCar.Length == 7)
                {
                    _car.CarModelName = _tempDescCar[0] + " " + _tempDescCar[1] + " " + _tempDescCar[2];
                    _car.Body = _tempDescCar[3];
                    _car.CC = Convert.ToInt32(_tempDescCar[4].Substring(0, _tempDescCar[4].Length - 2));
                    _car.Door = Convert.ToInt32(_tempDescCar[5]);
                }
                _car.GroupNo = Convert.ToInt32(GroupNo);
                _car.Info2 = CarVehicleType;

                //LC = 210 or 220
                //PS = 110 or 120
                //PU = 320
                //SV = 110 or 120
                if (CarVehicleType == "PS" || CarVehicleType == "SV" || CarVehicleType == "")
                {
                    _car.Info3 = VehicleType;
                }
                else if (CarVehicleType == "LC")
                {
                    if (VehicleType == "110")
                    {
                        _car.Info3 = "210";
                    }
                    else if (VehicleType == "120")
                    {
                        _car.Info3 = "220";
                    }
                }
                else if (CarVehicleType == "PU")
                {
                    if (VehicleType == "110")
                    {
                        _car.Info3 = "320";
                    }
                    else if (VehicleType == "120")
                    {
                        _car.Info3 = "340"; //Disabled, No Packages
                    }
                }
                _car.MakeModelDesc = CarMakeDesc + " " + CarModelDesc;
                _car.Status = true;
                _car.RefCEUserId = _refceuserid;
                _car.ViewedDetailId = 0;
                _car.CurrentStep = 1;
                _car.TotalStep = 4;
                _car.Info1 = "150";
                _carresult = _cardb.Insert(_car);
                _CarId = _carresult.ToString();
                _car = null;
                _cardb = null;

                #endregion

                #region CarPolicy
                if (_carresult > 0)
                {
                    HttpContext.Current.Session[CE.Global.General.CarId] = _carresult.ToString();
                }
                else
                {
                    //lblMessage.Text = "Insert error!";
                }
                if (_carresult > 0)
                {
                    //CAR POLICY DETAILS
                    CarPolicy _carpolicy = new CarPolicy();
                    CarPolicyDB _carpolicydb = new CarPolicyDB();
                    Int64 _carId = Convert.ToInt64(HttpContext.Current.Session[General.CarId].ToString());

                    Int64 _carpolicyid = 0;

                    if (HttpContext.Current.Session[General.CarPolicyId] != null)
                    {
                        _carpolicyid = Convert.ToInt64(HttpContext.Current.Session[General.CarPolicyId].ToString());
                        _carpolicy.CECarPolicyId = _carpolicyid;
                        if (HttpContext.Current.Session[General.AgentId] != null)
                        {
                            _carpolicy.Info5 = "UPDATE";
                        }
                    }
                    else
                    {
                        if (HttpContext.Current.Session[General.AgentId] != null)
                        {
                            _carpolicy.Info4 = "NEW";
                        }
                    }

                    //lblMessage.Text = "";
                    if (HttpContext.Current.Session[General.AgentId] != null)
                    {
                        _carpolicy.Info2 = HttpContext.Current.Session[General.AgentId].ToString();
                        _carpolicy.Info3 = General.GetUTCDateTime().ToString();
                    }
                    _carpolicy.CECarId = _carId;

                    int _carAge = (General.GetUTCDateTime().Year - Convert.ToInt32(Year)) + 1;

                    if (_carAge < 10)
                    {
                        _carpolicy.InsuranceType = 10;
                    }
                    else
                    {
                        _carpolicy.InsuranceType = 21;
                    }

                    _carpolicy.GarageType = 1;
                    _carpolicy.VehicleType = Convert.ToInt32(VehicleType);
                    _carpolicy.SumInsured = Convert.ToInt32(SumInsured.Replace(",", ""));
                    _carpolicy.ProvinceRegistration = "BKK";
                    _carpolicy.UseOfVehicle = "N/A";
                    string strDate = Commencemonth;
                    int _iDay = Convert.ToInt32(strcDate.Substring(0, 2));
                    int _iMonth = Convert.ToInt32(strDate.Substring(2, 2));
                    int _iYear = Convert.ToInt32(strDate.Substring(4, 4));
                    _carpolicy.CoverageCommenceDate = new DateTime(_iYear, _iMonth, _iDay);
                    _carpolicy.IsRedPlate = false;
                    _carpolicy.IsAddAccMod = false;

                    _carpolicy.NCDPercent = 20; //Default

                    _carpolicy.IsClaimLastYear = false;
                    _carpolicy.IsCTPL = false;

                    _carpolicy.IsAddDriver = false;

                    _carpolicy.Deductible = 0;
                    _carpolicy.CurrentInsurer = "Others";
                    _carpolicy.RefCEUserId = _refceuserid;
                    _carpolicy.ViewedDetailId = 0;
                    _carpolicy.Status = true;
                    _carpolicy.CurrentStep = 2;
                    _carpolicy.Info8 = "mobile";
                    _policyresult = _carpolicydb.Insert(_carpolicy);
                    if (_policyresult > 0)
                    {
                        _PolicyId = _policyresult.ToString();
                        HttpContext.Current.Session[General.CarPolicyId] = _policyresult.ToString();

                        MyDatabase _dbUpdate = new MyDatabase();
                        string _spNameUpdate = "sprocMobileCarInquiryDetailsInsertUpdateSingleItem";

                        SortedList _spListUpdate = new SortedList();
                        _spListUpdate.Add("@MobileCarInquiryId", Convert.ToInt64(_InquiryId));
                        _spListUpdate.Add("@CarId", _carresult);
                        _spListUpdate.Add("@CarPolicyId", _policyresult);
                        _spListUpdate.Add("@Step", 2);

                        object _retObjectUpdate = _dbUpdate.ExecuteSP(_spNameUpdate, _spListUpdate);
                    }
                    else
                    {
                        _PolicyId = "-1";
                    }
                    _carpolicy = null;
                    _carpolicydb = null;

                }
                #endregion

                //****** Yui ********
                if (URLFrom.ToLower() == "hmm")
                {
                    UpdateCarQuotePageSourceId(3, _carresult);
                }
                else if (URLFrom.ToLower() == "bcim")
                {
                    UpdateCarQuotePageSourceId(4, _carresult);
                }
            }
            HttpContext.Current.Session["FROMCARDETAIL"] = "1";
            return _result = _InquiryId + ":" + _CarId + ":" + _PolicyId;
        }
        catch (Exception ex)
        {
            return "-1:-1:-1";
        }
    }

    [WebMethod]
    public static string SaveMobileCarInquiry_old(string Year, string CarMake, string CarMakeDesc, string CarModel, string CarModelDesc, string Commencemonth,
        string GroupNo, string CarVehicleType, string VehicleType, string SumInsured)
    {
        try
        {
            #region Declaration
            string _InquiryId = "-1";
            string _CarId = "-1";
            string _PolicyId = "-1";
            string _result = "-1:-1:-1";
            Int64 _carresult = 0;
            Int64 _policyresult = 0;
            Int64 _refceuserid = 0;

            CarDetailTH _car = new CarDetailTH();
            CarDetailTHDB _cardb = new CarDetailTHDB();

            ProfileDB _profiledb = new ProfileDB();
            Profile _profile = new Profile();

            Contact _contact = new Contact();
            ContactDB _contactdb = new ContactDB();
            #endregion


            int _icDay = 1;
            string strcDate = Commencemonth;
            int _icMonth = Convert.ToInt32(strcDate.Substring(0, 2));
            int _icYear = Convert.ToInt32(strcDate.Substring(2, 4));
            DateTime CommencementDate = new DateTime(_icYear, _icMonth, _icDay);
            CarModelDesc = CarModelDesc.Replace('~', ' ');
            car_insurance_car_quote _objCarQuote = new car_insurance_car_quote();
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocMobileCarInquiryDetailsInsertUpdateSingleItem";

            SortedList _spList = new SortedList();
            if (HttpContext.Current.Session[General.InquiryId] != null)
            {
                _spList.Add("@MobileCarInquiryId", Convert.ToInt64(HttpContext.Current.Session[General.InquiryId].ToString()));
            }
            else
            {
                _spList.Add("@MobileCarInquiryId", 0);
            }
            _spList.Add("@Name", "");
            _spList.Add("@EmailAddress", "");
            _spList.Add("@PhoneNumber", "");
            _spList.Add("@CarMakeDescription", CarMakeDesc);
            _spList.Add("@CarMakeCode", CarMake);
            _spList.Add("@CarModelDescription", CarModelDesc);
            _spList.Add("@CarModelCode", CarModel);
            _spList.Add("@CarYear", Year);
            _spList.Add("@CarId", 0);
            _spList.Add("@CarPolicyId", 0);
            _spList.Add("@RefCEUserId", 0);
            _spList.Add("@SumInsured", SumInsured);
            _spList.Add("@VehicleType", VehicleType);
            _spList.Add("@CarVehicleType", CarVehicleType);
            _spList.Add("@GroupNo", GroupNo);
            _spList.Add("@CommencementDate", CommencementDate);
            _spList.Add("@InsuranceClass", "");
            _spList.Add("@Step", 1);

            object _retObject = _db.ExecuteSP(_spName, _spList);
            if (_retObject != null)
            {
                _InquiryId = Convert.ToInt64(_retObject).ToString();
                HttpContext.Current.Session[General.InquiryId] = _retObject.ToString();
            }

            if (Convert.ToInt64(_InquiryId) > 0)
            {
                #region CarDetails
                if (HttpContext.Current.Session[General.CarId] != null)
                {
                    _car = _cardb.GetCar(Convert.ToInt64(HttpContext.Current.Session[General.CarId].ToString()), 0);
                    if (HttpContext.Current.Session[General.AgentId] != null)
                    {
                        _car.Info5 = "UPDATE";
                    }
                }
                else
                {
                    if (HttpContext.Current.Session[General.AgentId] != null)
                    {
                        _car.Info4 = "NEW";
                    }
                }

                if (_car == null)
                {
                    _car = new CarDetailTH();
                }

                if (HttpContext.Current.Session[CE.Global.General.UserId] != null)
                {
                    _refceuserid = Convert.ToInt64(HttpContext.Current.Session[CE.Global.General.UserId]);
                    _profile = _profiledb.GetProfile(_refceuserid);
                    _contact = _contactdb.GetContact(_refceuserid);
                    if (_profile != null && _contact != null)
                    {
                        _car.Info5 = _contact.Email;
                        _car.Info6 = _contact.Mobile;
                        _car.Info7 = _profile.SurName;
                        if (_profile.FirstName.Trim().Length > 0)
                        {
                            _car.Info8 = _profile.FirstName;
                        }
                        _car.Info9 = _profile.Gender.ToString();
                        _car.Info10 = _profile.DOB.ToString("dd-MMM-yyyy");
                        _car.Info11 = _profile.Occupation.ToString();
                        _car.Info12 = _profile.JobNature;
                        _car.Info13 = _profile.MaritalStatus.ToString();
                        _car.Info14 = "0";
                        _car.Info15 = _profile.Salutation;
                        _profile = null;
                        _contact = null;
                    }
                }

                _car.ManufactYear = Convert.ToInt32(Year);

                _car.CarMakeName = CarMake;
                string[] _tempDesc = CarModelDesc.Split(' ');
                if (_tempDesc.Length == 5)
                {
                    _car.CarModelName = _tempDesc[0];
                    _car.Body = _tempDesc[1];
                    _car.CC = Convert.ToInt32(_tempDesc[2].Substring(0, _tempDesc[2].Length - 2));
                    _car.Door = Convert.ToInt32(_tempDesc[3]);
                }
                else if (_tempDesc.Length == 6)
                {
                    _car.CarModelName = _tempDesc[0] + " " + _tempDesc[1];
                    _car.Body = _tempDesc[2];
                    _car.CC = Convert.ToInt32(_tempDesc[3].Substring(0, _tempDesc[3].Length - 2));
                    _car.Door = Convert.ToInt32(_tempDesc[4]);
                }
                else if (_tempDesc.Length == 7)
                {
                    _car.CarModelName = _tempDesc[0] + " " + _tempDesc[1] + " " + _tempDesc[2];
                    _car.Body = _tempDesc[3];
                    _car.CC = Convert.ToInt32(_tempDesc[4].Substring(0, _tempDesc[4].Length - 2));
                    _car.Door = Convert.ToInt32(_tempDesc[5]);
                }
                _car.GroupNo = Convert.ToInt32(GroupNo);
                _car.Info2 = CarVehicleType;

                //LC = 210 or 220
                //PS = 110 or 120
                //PU = 320
                //SV = 110 or 120
                if (CarVehicleType == "PS" || CarVehicleType == "SV" || CarVehicleType == "")
                {
                    _car.Info3 = VehicleType;
                }
                else if (CarVehicleType == "LC")
                {
                    if (VehicleType == "110")
                    {
                        _car.Info3 = "210";
                    }
                    else if (VehicleType == "120")
                    {
                        _car.Info3 = "220";
                    }
                }
                else if (CarVehicleType == "PU")
                {
                    if (VehicleType == "110")
                    {
                        _car.Info3 = "320";
                    }
                    else if (VehicleType == "120")
                    {
                        _car.Info3 = "340"; //Disabled, No Packages
                    }
                }
                _car.MakeModelDesc = CarMakeDesc + " " + CarModelDesc;
                _car.Status = true;
                _car.RefCEUserId = _refceuserid;
                _car.ViewedDetailId = 0;
                _car.CurrentStep = 1;
                _car.TotalStep = 4;
                _car.Info1 = "150";
                _carresult = _cardb.Insert(_car);
                _CarId = _carresult.ToString();
                _car = null;
                _cardb = null;

                #endregion

                #region CarPolicy
                if (_carresult > 0)
                {
                    HttpContext.Current.Session[CE.Global.General.CarId] = _carresult.ToString();
                }
                else
                {
                    //lblMessage.Text = "Insert error!";
                }
                if (_carresult > 0)
                {
                    //CAR POLICY DETAILS
                    CarPolicy _carpolicy = new CarPolicy();
                    CarPolicyDB _carpolicydb = new CarPolicyDB();
                    Int64 _carId = Convert.ToInt64(HttpContext.Current.Session[General.CarId].ToString());

                    Int64 _carpolicyid = 0;

                    if (HttpContext.Current.Session[General.CarPolicyId] != null)
                    {
                        _carpolicyid = Convert.ToInt64(HttpContext.Current.Session[General.CarPolicyId].ToString());
                        _carpolicy.CECarPolicyId = _carpolicyid;
                        if (HttpContext.Current.Session[General.AgentId] != null)
                        {
                            _carpolicy.Info5 = "UPDATE";
                        }
                    }
                    else
                    {
                        if (HttpContext.Current.Session[General.AgentId] != null)
                        {
                            _carpolicy.Info4 = "NEW";
                        }
                    }

                    //lblMessage.Text = "";
                    if (HttpContext.Current.Session[General.AgentId] != null)
                    {
                        _carpolicy.Info2 = HttpContext.Current.Session[General.AgentId].ToString();
                        _carpolicy.Info3 = General.GetUTCDateTime().ToString();
                    }
                    _carpolicy.CECarId = _carId;

                    int _carAge = (General.GetUTCDateTime().Year - Convert.ToInt32(Year)) + 1;

                    if (_carAge < 10)
                    {
                        _carpolicy.InsuranceType = 10;
                    }
                    else
                    {
                        _carpolicy.InsuranceType = 21;
                    }

                    _carpolicy.GarageType = 1;
                    _carpolicy.VehicleType = Convert.ToInt32(VehicleType);
                    _carpolicy.SumInsured = Convert.ToInt32(SumInsured.Replace(",", ""));
                    _carpolicy.ProvinceRegistration = "BKK";
                    _carpolicy.UseOfVehicle = "N/A";
                    int _iDay = 1;
                    string strDate = Commencemonth;
                    int _iMonth = Convert.ToInt32(strDate.Substring(0, 2));
                    int _iYear = Convert.ToInt32(strDate.Substring(2, 4));
                    _carpolicy.CoverageCommenceDate = new DateTime(_iYear, _iMonth, _iDay);
                    _carpolicy.IsRedPlate = false;
                    _carpolicy.IsAddAccMod = false;

                    _carpolicy.NCDPercent = 20; //Default

                    _carpolicy.IsClaimLastYear = false;
                    _carpolicy.IsCTPL = false;

                    _carpolicy.IsAddDriver = false;

                    _carpolicy.Deductible = 0;
                    _carpolicy.CurrentInsurer = "Others";
                    _carpolicy.RefCEUserId = _refceuserid;
                    _carpolicy.ViewedDetailId = 0;
                    _carpolicy.Status = true;
                    _carpolicy.CurrentStep = 2;
                    _carpolicy.Info8 = "mobile";
                    _policyresult = _carpolicydb.Insert(_carpolicy);
                    if (_policyresult > 0)
                    {
                        _PolicyId = _policyresult.ToString();
                        HttpContext.Current.Session[General.CarPolicyId] = _policyresult.ToString();

                        MyDatabase _dbUpdate = new MyDatabase();
                        string _spNameUpdate = "sprocMobileCarInquiryDetailsInsertUpdateSingleItem";

                        SortedList _spListUpdate = new SortedList();
                        _spListUpdate.Add("@MobileCarInquiryId", Convert.ToInt64(_InquiryId));
                        _spListUpdate.Add("@CarId", _carresult);
                        _spListUpdate.Add("@CarPolicyId", _policyresult);
                        _spListUpdate.Add("@Step", 2);

                        object _retObjectUpdate = _dbUpdate.ExecuteSP(_spNameUpdate, _spListUpdate);
                    }
                    else
                    {
                        _PolicyId = "-1";
                    }
                    _carpolicy = null;
                    _carpolicydb = null;
                }
                #endregion
            }
            return _result = _InquiryId + ":" + _CarId + ":" + _PolicyId;
        }
        catch (Exception ex)
        {
            return "-1:-1:-1";
        }
    }

    [WebMethod]
    public static string CarModelClickChanged_Old()
    {
        try
        {
            string result = "";
            DataTable _dtblMonth = new DataTable();
            _dtblMonth.Clear();
            _dtblMonth.Columns.Add("MonthId");
            _dtblMonth.Columns.Add("Month");
            DateTime dtNow = new DateTime();
            dtNow = General.GetUTCDateTime().AddDays(14);

            DateTime dtEndMonth = new DateTime();
            dtEndMonth = General.GetUTCDateTime().AddDays(120);

            int mthDiff = 12 - dtNow.Month;
            for (int m = 0; m <= mthDiff; m++)
            {
                DateTime dtTemp = dtNow.AddMonths(m);
                string strDate = dtTemp.Date.Month.ToString("00") + dtTemp.Date.Year.ToString("0000");

                DataRow _dtblRow = _dtblMonth.NewRow();
                _dtblRow["MonthId"] = strDate.ToString();
                _dtblRow["Month"] = dtTemp.ToString("MMMM yyyy").ToString();
                _dtblMonth.Rows.Add(_dtblRow);
            }
            result = "<ul class='select-box-2' id='Commencement'>";
            foreach (DataRow row in _dtblMonth.Rows)
            {
                result += "<li onclick='cm(this);' rel=" + row["MonthId"].ToString() + ">";
                result += row["Month"].ToString();
                result += "</li>";
            }
            return result += "</ul>";
        }
        catch (Exception ex)
        {
            return "";
        }
    }

    [WebMethod]
    public static string CarMakeClickChanged_Old(string CarMake, string CarMakeDesc, string Year)
    {
        try
        {
            string result = "";
            string s = CarMakeDesc;
            MyDatabase _db = new MyDatabase();
            DataTable _dt = new DataTable("tblModelDesc");
            SortedList _spList = new SortedList();
            _spList.Add("@CarMake", CarMake);
            _spList.Add("@Year", Year);
            _dt = _db.GetDataTableBySP("sprocGetCarModelDescTHListByCarMakeCode", _spList);
            if (_dt != null && _dt.Rows.Count > 0)
            {
                result = "<ul class='select-box-2' id='CarModel'>";
                foreach (DataRow row in _dt.Rows)
                {
                    result += "<li class='text-link' onclick='mo(this);' rel=" + row["CarModelDesc"].ToString() + ">";
                    result += row["CarModelDesc"].ToString();
                    //result += row["CarModelDisplay"].ToString();
                    result += "</li>";
                }
                result += "</ul>";
            }
            _dt = null;
            _db = null;
            return result;
        }
        catch (Exception ex)
        {
            return "";
        }
    }

    protected void ibtnNewQuote_Click(object sender, ImageClickEventArgs e)
    {
        CE.Global.General.ClearAllSession(this.Page, "CI");
    }

    private void LogUserViewedHistory(string PageType, string Description)
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory(PageType, Request, Description);
    }

    public static Int64 UpdateCarQuotePageSourceId(int SourceId, Int64 CarId)
    {
        try
        {
            MyDatabase _dbUpdate = new MyDatabase();
            SortedList _spList = new SortedList();
            string _spName = "sprocCarQuotePageSourceIdUpdateSingleItem";

            _spList.Add("@CarId", CarId);
            _spList.Add("@SourceId", SourceId);

            object _retObject = _dbUpdate.ExecuteSP(_spName, _spList);
            return Convert.ToInt64(_retObject);
        }
        catch (Exception ex)
        {
            return -1;
        }
    }

}
